package ca.swank.hardquor;

import java.util.ArrayList;
// import java.util.Comparable;

public class WalkMove extends Move implements Comparable
{ private int[] source, target;

  public WalkMove(int[] source, int[] target)
  { this.source = source;
    this.target = target;
  }

  public int[] getSource() { return source; }
  public int[] getTarget() { return target; }
  public int[] source() { return getSource(); }
  public int[] target() { return getTarget(); }

  public String toString()
  { return "WALK[" + coordToString(source) + " " + coordToString(target) + "]";
  }

  public int getDirection()
  { if(source[1] < target[1]) return hardquorBoard.DIR_ABOVE;
    else if(source[0] < target[0]) return hardquorBoard.DIR_RIGHT;
    else if(source[1] > target[1]) return hardquorBoard.DIR_BELOW;
    else if(source[0] > target[0]) return hardquorBoard.DIR_LEFT;
    else return hardquorBoard.DIR_CRISSCROSS; // to signal an error
  }
  
  public boolean isHorizontal()
  { return (source[0] != target[0]);
  }

  public boolean isVertical()
  { return (source[1] != target[1]);
  }
  
  public WallMove[] getBlockingWalls()
  { 
    ArrayList blockingWallMoves = new ArrayList();

    if(isHorizontal())
    { blockingWallMoves.add(new WallMove(source, new int[] { source[0], source[1] + 1 }, target, new int[] { target[0], target[1] + 1 }));
      blockingWallMoves.add(new WallMove(source, new int[] { source[0], source[1] - 1 }, target, new int[] { target[0], target[1] - 1 }));
    }
    if(isVertical())
    { blockingWallMoves.add(new WallMove(source, new int[] { source[0] + 1, source[1] }, target, new int[] { target[0] + 1, target[1] }));
      blockingWallMoves.add(new WallMove(source, new int[] { source[0] - 1, source[1] }, target, new int[] { target[0] - 1, target[1] }));
    }
    
    return (WallMove[])blockingWallMoves.toArray(new WallMove[] { });
  
  }
  
  public int compareTo(Object o)
  { if(!(o instanceof WalkMove)) return -1;
    WalkMove other = (WalkMove)o;
    int result;
    if((result = coordCompare.compare(source, other.source)) != 0) return result;
    else return coordCompare.compare(target, other.target);
  }
  
  public int compareTo2(Object o)
  { if(!(o instanceof WalkMove)) return -1;
    WalkMove other = (WalkMove)o;
    int res = 0;
    
    if(this.source[0] != other.source[0]) res = -1;
    if(this.source[1] != other.source[1]) res = -1;
    if(this.target[0] != other.target[0]) res = -1;
    if(this.target[1] != other.target[1]) res = -1;
    
    return res;
  }
  
}

